//
//  TextView.swift
//  OttCapital
//
//  Created by 陈宇 on 2017/4/7.
//  Copyright © 2017年 OttCapital. All rights reserved.
//

import UIKit
import RxSwift

class TextView: UITextView {
    
    lazy var disposeBag = DisposeBag()
    
    fileprivate let placeholderLabel = UILabel()
    
    /// A reference to the textView text.
    open override var text: String! {
        didSet {
            setContentOffset(.zero, animated: true)
            updatePlaceholderVisibility()
        }
    }
    
    override var font: UIFont? {
        didSet {
            setContentOffset(.zero, animated: true)
            placeholderLabel.font = font
        }
    }
    
    /// The placeholderLabel text value.
    @IBInspectable
    open var placeholder: String? {
        get {
            return placeholderLabel.text
        }
        set(value) {
            placeholderLabel.text = value
        }
    }
    
    /// Placeholder normal text
    @IBInspectable
    open var placeholderNormalColor = UIColor.black.withAlphaComponent(0.38) {
        didSet {
            placeholderLabel.textColor = placeholderNormalColor
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        ovserverTextChanged()
        preparePlaceholderLabel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        ovserverTextChanged()
        preparePlaceholderLabel()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutPlaceholderLabel()
    }
    
    func ovserverTextChanged() {
        rx.text.changed.subscribe(onNext: { [weak self] (string) in
            self?.updatePlaceholderVisibility()
        }).addDisposableTo(disposeBag)
    }
    
}

extension TextView {
    
    /// prepares the placeholderLabel property.
    fileprivate func preparePlaceholderLabel() {
        placeholderLabel.textColor = placeholderNormalColor
        placeholderLabel.textAlignment = textAlignment
        placeholderLabel.numberOfLines = 0
        placeholderLabel.backgroundColor = .clear
        placeholderLabel.font = font
        addSubview(placeholderLabel)
    }
    
    fileprivate func layoutPlaceholderLabel() {
        placeholderLabel.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2
        
        let x = textContainerInset.left + textContainer.lineFragmentPadding
        let y = textContainerInset.top
        placeholderLabel.sizeToFit()
        
        placeholderLabel.frame.origin.x = x
        placeholderLabel.frame.origin.y = y
        placeholderLabel.frame.size.width = textContainer.size.width - textContainerInset.right - textContainer.lineFragmentPadding
    }
    
    /// Updates the placeholderLabel visibility.
    fileprivate func updatePlaceholderVisibility() {
        placeholderLabel.isHidden = !isEmpty
    }
    
    open var isEmpty: Bool {
        return 0 == text?.utf16.count
    }
}
